public without sharing class SMSPrepareController{
    public List<ContactBean> cbList { get; set; }
    public List<Contact> selectedList { get; set; }
    // public List<SMSTemplate__c> smsList { get; set; }
    //查询时下拉列表框选择的模板名
    public String tmpName { get; set; }
    public SMSTemplate__c tmp { get; set; }
    public Contact con { get; set; }
    public Account acc { get; set; }
    public Integer conNum { get; private set; }

    //要发送的短信内容
    public String smsMessageText {get;set;}

    public SMSPrepareController() {
        tmp = new SMSTemplate__c();
        cbList = new List<ContactBean>();
        selectedList = new List<Contact>();
        // smsList = new List<SMSTemplate__c>();
        con = new Contact();
        acc = new Account();
        conNum = 0;
    }

    public void init() {
        //searchTemplate();
    }

    public void searchContact() {
        System.debug('*** 已设置的检索信息(职位): ' + con);
        System.debug('*** 已设置的检索信息(省市): ' + acc);

        // String conAccId = '';
        // String conDept = '';
        // String conSpec = '';
        // String conPost = '';
        // String conProv = '';
        // String conCity = '';
        // try{
        //     conAccId = con.AccountId != null?con.AccountId :'';
        // }catch(Exception e){}
        // try{
        //     conDept = con.Departments__c != null?con.Departments__c :'';
        // }catch(Exception e){}
        // try{
        //     conSpec = con.SpecialTeam__c != null?con.SpecialTeam__c :'';
        // }catch(Exception e){}
        // try{
        //     conPost = con.Post__c != null?con.Post__c :'';
        // }catch(Exception e){}
        // try{
        //     conProv = acc.Province__c != null?acc.Province__c :'';
        // }catch(Exception e){}
        // try{
        //     conCity = acc.City__c != null?acc.City__c :'';
        // }catch(Exception e){}

        String sql = 'SELECT Id, Name,MobilePhone,AccountId,Account.Name,Account.Province__c,';
        sql += ' Departments__c, SpecialTeam__c, Post__c FROM Contact WHERE Name <> null AND MobilePhone <> null ';
        if(!String.isBlank(con.AccountId)){
            sql += ' AND AccountId = \'' + con.AccountId + '\'';
        }
        if(!String.isBlank(con.Departments__c)){
            sql += ' AND Departments__c =\'' + con.Departments__c + '\'';
        }
        if(!String.isBlank(con.SpecialTeam__c)){
            sql += ' AND SpecialTeam__c =\'' + con.SpecialTeam__c + '\'';
        }
        if(!String.isBlank(con.Post__c)){
            sql += ' AND Post__c =\'' + con.Post__c + '\'';
        }
        if(!String.isBlank(acc.Province__c)){
            sql += ' AND Account.Province__c =\'' + acc.Province__c + '\'';
        }
        if(!String.isBlank(acc.City__c)){
            sql += ' AND Account.City__c =\'' + acc.City__c + '\'';
        }
        sql += ' LIMIT 100';

        System.debug('*** 联系人检索SOQL: ' + sql);
        cbList = new List<ContactBean>();
        for (Contact item : Database.query(sql)) { 
            cbList.add(new ContactBean(item));
        }
    }

/*    public void searchTemplate() {
        String sql = 'select Name, Type__c, Body__c from SMSTemplate__c';
        smsList = Database.query(sql);
    }*/

    public void selectContact() {
        Map<Id, Id> dupliMap = new Map<Id, Id>();
        for (Contact c : selectedList) {
            dupliMap.put(c.Id, c.Id);
        }
        for (ContactBean cb : cbList) {
            if (cb.chk == true && dupliMap.containsKey(cb.rec.Id) == false) {
                selectedList.add(cb.rec);
            }
        }
        conNum = selectedList.size();
    }

    public void verifyTemplate() {
        if(selectedList.size()>0){
            smsMessageText = Utils.getSMSTemplate(tmp.Type__c);
            smsMessageText = smsMessageText.replace('{ContactName}', selectedList[0].Name);
        }
    }

    public void sendSms() {
        if(selectedList.size()>0){
            String sms = Utils.getSMSTemplate(tmp.Type__c);
            for (Contact con : selectedList) {
                Utils.sendSmsMessage(con.MobilePhone,sms.replace('{ContactName}', con.Name)); 
            }
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'短信发送成功！'));
        }
    }
    
    public class ContactBean {
        public Contact rec { get; set; }
        public Boolean chk { get; set; }

        public ContactBean(Contact record) {
            rec = record;
            chk = false;
        }
    }
}